<script>
$("#swipe_card").click(function(){
    layer.open({
        type: 2
        , skin: 'layui-layer-molv'
        , title: false
        , shadeClose: true
        , shade: 0.2
        , id: 'search_archives_dialog'
        , area:['800px', '500px']
        , content: ['{{urlfor "ArchivesController.EditForOpt"}}?/0']
        , anim: 1 //动画类型
    });
    return false;
});    

function receiveFormData(d) {
    if(!d) return false;

    for (var key in d){
        val = d[key]
        if(key == "Sex") {
            $('input[name="Sex"]').each(function(){ if($(this).attr("value") == val) { $(this).prop("checked", true) }  });
        }else if(key =="Id") {
            $('#arch_id').val(d.Id);
        }else if(key =="CreatedBy") {

        }else{
            $("#"+key).val(val)
        }
    }
    $('select').trigger('changed.selected.amui');
    formValidatorReset('formDoSubmitThis');
    layer.closeAll();
}

//处方类型切换
$("#chufang_info").on("click", ".chufang > .title > ul > li > a", function() {
    var type = $(this).data("type");
    $(".chufang > .title > ul > li > a").removeClass("active");
    $(this).addClass("active");
    $(".chufang > .datatable-shili").hide();
    $(".chufang > .datatable-shili[data-type='" + type + "']").show();
});
//处方内容清除
$("#chufang_info").on("click", ".chufang > .datatable-shili > .clear-btn", function() {
    $(this).parent().attr("saved","fales");
    $(this).parent().find(".info-bar[data-type] > .item").text("");
    $(this).parent().find(".info-bar.define-row > .item > .pmr > b").text("");
});
//验光处方具体光度块点击
$("#chufang_info").on("blur", ".chufang > .datatable-shili > .info-bar > .item[data-type]", function() {
    var box = $("#chufang_info");
    var p_type=$(this).parent().parent().attr("data-type");
    var d_type = $(this).data('type');
    var new_va = $(this).text();
    if(new_va == '') {
        return;
    }
    box.find("div.chufang").find(".datatable-shili[data-type='"+p_type+"']").attr("saved","true");
    if((p_type=="far"||p_type=="near"||p_type=="duojiaodian")&&(d_type=="pd_yuan"||d_type=="pd_jin")){
        box.find("div.chufang").find(".datatable-shili[data-type='"+p_type+"']").find("div[data-type='left']").next().find(".item > .pmr[data-type='total_"+d_type+"'] > b").text("");
        var total=0;
        $(this).parents(".datatable-shili").find("div.item[data-type='"+d_type+"']").each(function(){
            total = Number($(this).text())+total;
        });
        box.find("div.chufang").find(".datatable-shili[data-type='"+p_type+"']").find("div[data-type='left']").next().find(".item > .pmr[data-type='total_"+d_type+"'] > b").text(total);
    }

    if(d_type=="sph"||d_type=="cyl"){//球镜柱镜数值处理下
        new_va = formatnumber(new_va, "0.00");
        if(new_va>0){
            new_va = "+"+new_va;
        }else if(new_va==0){
            new_va  = new_va.toString()+".00";
        }
    }else if(d_type=="nv"||d_type=="cv"){
        new_va = formatnumber(new_va, "0.0");
    }
    $(this).text(new_va);
});

$('#submit_btn').click(function(){
	$('#formDoSubmitThis').submit();
	return false;
});
$('#formDoSubmitThis').submit(function(){
	var formValidity = $(this).validator('isFormValid');
	if(formValidity) { //通过检查
        save();
	}
	return false;	
});

function save() {
    var data={};
    /*****************组装验光数据**********************/
    data.optometry_and_optician_info={};
    var far =[];
    var naer=[];
    var yinxing=[];
    var dojiaodian=[];
    data.optometry_and_optician_info.far_info=far;
    data.optometry_and_optician_info.near_info =naer;
    data.optometry_and_optician_info.yinxing_info=yinxing;
    data.optometry_and_optician_info.duojiaodian_info=dojiaodian;
    var o_json ={};
    var box = $("#chufang_info");
    box.find("div.chufang").find(".datatable-shili[data-type='far'][saved='true']").find(".info-bar").each(function(){
        if($(this).attr("data-type")){
            o_json={};
            o_json.type_name = 'far';
            o_json.sph = $(this).find("div[data-type='sph']").text();
            o_json.cyl = $(this).find("div[data-type='cyl']").text();
            o_json.axial_view = $(this).find("div[data-type='axial_view']").text();
            o_json.pd_yuan= $(this).find("div[data-type='pd_yuan']").text();
            o_json.mrp = $(this).find("div[data-type='mrp']").text();
            o_json.nv = $(this).find("div[data-type='nv']").text();
            o_json.cv = $(this).find("div[data-type='cv']").text();
            o_json.v = $(this).find("div[data-type='v']").text();
            o_json.bc = $(this).find("div[data-type='bc']").text();
            o_json.total_pd_yuan = box.find("div.chufang").find(".datatable-shili[data-type='far'][saved='true']").find(".info-bar:last").find("[data-type='total_pd_yuan']").find("b").text();
            if($(this).attr("data-type")=="left"){
                o_json.eye_type = 'left';
            }else if($(this).attr("data-type")=="right"){
                o_json.eye_type = 'right';
            }
            far.push(o_json);
        }
    });

    box.find("div.chufang").find(".datatable-shili[data-type='near'][saved='true']").find(".info-bar").each(function(){
        if($(this).attr("data-type")){
            o_json={};
            o_json.type_name = 'near';
            o_json.sph = $(this).find("div[data-type='sph']").text();
            o_json.cyl = $(this).find("div[data-type='cyl']").text();
            o_json.axial_view = $(this).find("div[data-type='axial_view']").text();
            o_json.pd_jin= $(this).find("div[data-type='pd_jin']").text();
            o_json.mrp = $(this).find("div[data-type='mrp']").text();
            o_json.nv = $(this).find("div[data-type='nv']").text();
            o_json.cv = $(this).find("div[data-type='cv']").text();
            o_json.v = $(this).find("div[data-type='v']").text();
            o_json.bc = $(this).find("div[data-type='bc']").text();
            o_json.total_pd_jin = box.find("div.chufang").find(".datatable-shili[data-type='near'][saved='true']").find(".info-bar:last").find("[data-type='total_pd_jin']").find("b").text();
            if($(this).attr("data-type")=="left"){
                o_json.eye_type = 'left';
            }else if($(this).attr("data-type")=="right"){
                o_json.eye_type = 'right';
            }
            naer.push(o_json);
        }
    });

    box.find("div.chufang").find(".datatable-shili[data-type='yinxing'][saved='true']").find(".info-bar").each(function(){
        if($(this).attr("data-type")){
            o_json={};
            o_json.type_name = 'yinxing';
            o_json.sph = $(this).find("div[data-type='sph']").text();
            o_json.cyl = $(this).find("div[data-type='cyl']").text();
            o_json.axial_view = $(this).find("div[data-type='axial_view']").text();
            o_json.pd_jin= $(this).find("div[data-type='pd_jin']").text();
            o_json.mrp = $(this).find("div[data-type='mrp']").text();
            o_json.nv = $(this).find("div[data-type='nv']").text();
            o_json.cv = $(this).find("div[data-type='cv']").text();
            o_json.diameter = $(this).find("div[data-type='diameter']").text();
            o_json.base_curve = $(this).find("div[data-type='base_curve']").text();

            if($(this).attr("data-type")=="left"){
                o_json.eye_type = 'left';
            }else if($(this).attr("data-type")=="right"){
                o_json.eye_type = 'right';
            }
            yinxing.push(o_json);
        }
    });
    box.find("div.chufang").find(".datatable-shili[data-type='duojiaodian'][saved='true']").find(".info-bar").each(function(){
        if($(this).attr("data-type")){
            o_json={};
            o_json.type_name = 'duojiaodian';
            o_json.sph = $(this).find("div[data-type='sph']").text();
            o_json.cyl = $(this).find("div[data-type='cyl']").text();
            o_json.axial_view = $(this).find("div[data-type='axial_view']").text();
            o_json.pd_jin= $(this).find("div[data-type='pd_jin']").text();
            o_json.pd_yuan= $(this).find("div[data-type='pd_yuan']").text();
            o_json.mrp = $(this).find("div[data-type='mrp']").text();
            o_json.nv = $(this).find("div[data-type='nv']").text();
            o_json.cv = $(this).find("div[data-type='cv']").text();
            o_json.add_info = $(this).find("div[data-type='add_info']").text();
            o_json.pass = $(this).find("div[data-type='pass']").text();
            o_json.v = $(this).find("div[data-type='v']").text();
            o_json.bc = $(this).find("div[data-type='bc']").text();
            o_json.total_pd_jin = box.find("div.chufang").find(".datatable-shili[data-type='duojiaodian'][saved='true']").find(".info-bar:last").find("[data-type='total_pd_jin']").find("b").text();
            o_json.total_pd_yuan = box.find("div.chufang").find(".datatable-shili[data-type='duojiaodian'][saved='true']").find(".info-bar:last").find("[data-type='total_pd_yuan']").find("b").text();
            if($(this).attr("data-type")=="left"){
                o_json.eye_type = 'left';
            }else if($(this).attr("data-type")=="right"){
                o_json.eye_type = 'right';
            }
            dojiaodian.push(o_json);
        }
    });

    /****************************************/
    if (data.optometry_and_optician_info.duojiaodian_info.length <= 0
        && data.optometry_and_optician_info.yinxing_info.length <= 0
        && data.optometry_and_optician_info.near_info.length <= 0
        && data.optometry_and_optician_info.far_info.length <= 0) {
        layer.alert("请填写处方详情");
        return;
    }

    var arch_id = $('#arch_id').val();
    var doctor = $('#Doctor').val();
    var optometrist = $('#Optometrist').val();
    var remarks = $('#remarks').val();
    if(arch_id == 0) {
        layer.alert("请选择一个会员档案");
        return;
    }
    var _data = {};
    _data.arch_id = arch_id;
    _data.doctor = doctor;
    _data.optometrist = optometrist;
    _data.remarks = remarks;

    _data.chufang = $.merge([], data.optometry_and_optician_info.duojiaodian_info);
    _data.chufang = $.merge(_data.chufang, data.optometry_and_optician_info.yinxing_info);
    _data.chufang = $.merge(_data.chufang, data.optometry_and_optician_info.near_info);
    _data.chufang = $.merge(_data.chufang, data.optometry_and_optician_info.far_info);
    _data.chufang = JSON.stringify(_data.chufang);

   $.post('{{ urlfor "OptometryController.Add" }}', _data, function (res) {
       if(res.code == 1) {
           layer.alert(res.msg);
           return false;
       }
       layer.alert('验光处方保存成功', function () {
           location.reload();
       })
   });
}

function searcharchives() {
    layer.open({
        type: 2
        , skin: 'layui-layer-molv'
        , title: false
        , shadeClose: false
        , shade: 0.2
        , id: 'search_archives_dialog'
        , area: ['1000px', '600px']
        , content: '{{urlfor "ArchivesController.SelectArchives"}}'
        , anim: 1 //动画类型
        ,end: function(){
            if($('#arch_id').val() == 0) {
                location.href = '{{ urlfor "OptometryController.Index" }}';
            }
        }
    });
}
searcharchives();
</script>